package sel.me.websocket.learning.controller.websocket;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Controller;

import sel.me.websocket.learning.entity.PushMessage;

/**
 * Created by rajeevkumarsingh on 24/07/17.
 */
@Controller
public class ChatController {

	@Autowired
    private SimpMessageSendingOperations messagingTemplate;
	
    @MessageMapping("/chat.sendMessageByUser.{groupUuid}.{uuid}")
//    @SendTo("/topic/netbar/group/{groupUuid}")
    public void sendMessage(@Payload PushMessage chatMessage, @DestinationVariable String groupUuid, @DestinationVariable String uuid) {
//    	AdminUserDO admin =  (AdminUserDO)SecurityUtils.getSubject().getSession().getAttribute("currentUser");
//        System.out.println(admin);
    	messagingTemplate.convertAndSend("/topic/websocketbbs/member", chatMessage);
    	messagingTemplate.convertAndSend("/topic/websocketbbs/user", chatMessage);
    }

    @MessageMapping("/chat.addUser")
    @SendTo("/topic/websocketbbs/public")
    public PushMessage addUser(@Payload PushMessage chatMessage,
                               SimpMessageHeaderAccessor headerAccessor) {
        // Add username in web socket session
        headerAccessor.getSessionAttributes().put("username", chatMessage.getSender());
        return chatMessage;
    }

}
